﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Threading;

namespace Zeus.Thunder.Web.Controllers
{
    public class CacheController : Controller
    {
        public ActionResult SessionScopeCache()
        {
            if (HttpContext.Session["IsFirstTimeUser"] == null)
            {
                HttpContext.Session["IsFirstTimeUser"] = true;
            }  
            else
            {
                HttpContext.Session["IsFirstTimeUser"] = false;
            }

            return RedirectToAction("SessionCacheReceiver");
        }

        public ActionResult SessionCacheReceiver()
        {
            bool IsFirstTimeUser = (bool)HttpContext.Session["IsFirstTimeUser"];

            ViewBag.FirstTimeUser = IsFirstTimeUser;

            return View("/Views/Other/Cache/SessionCacheReceiver.cshtml");
        }

        [OutputCache(Duration = 60, VaryByParam = "none")]
        public ActionResult OutputCacheTest()
        {
            ViewBag.CurrentTime = DateTime.Now.ToString();
            return View("/Views/Other/Cache/OutputCacheReceiver.cshtml");
        }

        [OutputCache(Duration = 3600, VaryByParam = "id")]
        public ActionResult OutputByParam(string id)
        {
            var result = GetResult(id);
            ViewBag.CacheResult = result;
            return View("/Views/Other/Cache/OutputByParamReceiver.cshtml");
        }

        [NonAction]
        private string GetResult(string id)
        {
           Thread.Sleep(5000);

            return "Output Result: " + id;
        }
    }
}
